<?php
namespace app\index\controller;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
    	// 一级分类
    	$cate = db('cate')->field('id,catename')->where('pid', 0)->select();
    	$count = db('cate')->where('pid', 0)->count();	

    	// 一级分类的子类
    	for ($i=0; $i < $count; $i++) { 
    		$first = db('cate')->field('id,catename,image')->where('pid',$cate[$i]['id'])->select();
            $num = db('cate')->where('pid',$cate[$i]['id'])->count();
            // 每个区的帖子数
            foreach ($first as &$val) {
              $val['number'] = db('note')->where(['cateid'=>$val['id'],'status'=>'0'])->count();
    		  $val['new'] = db('note')->where(['cateid'=>$val['id'],'status'=>'0'])->field('id,time')->order('id','desc')->limit(1)->find(); 
              if ($val['new'] != '') {
                  $val['new']['time'] = date('Y-m-d H:i',$val['new']['time']);
              }
              
            }
    		$cate[$i]['num'] = $num;
            $cate[$i]['son'] = $first;
        }
    	// echo "<pre>";
    	// 	print_r($cate);
    	// echo "</pre>";die;

        $picture = db('picture')->where('status',1)->limit(4)->select();
        $this->assign("picture",$picture);

        // 所有帖子查询
        $notenum = db('note')->where('status','0')->count('*'); 
        $this->assign("notenum",$notenum);

        // 所有用户查询
        $usernum = db('user')->count('*');
        $this->assign("usernum",$usernum);
        $this->assign('cate',$cate);
        $this->assign('title','论坛首页');
        $link = db('link')->select();
    	$advert1 = db('advert')->where('id'.'='.'1')->find();
    	$advert2 = db('advert')->where('id'.'='.'2')->find();
    	$news = db('news')->select();
        $sign = db('signin')->where('uid',session('user')['id'])->find();
		if ($sign == '') {
            $this->assign('signin', '签到');
        }else{
            $this->assign('signin', '已签到');
        }
        $this->assign('link', $link);
//		dump($link);die;
		$this->assign('advert1', $advert1);
		$this->assign('advert2', $advert2);
		$this->assign('news', $news);
//				dump($news);die;
        // 所有版主查询
        $banzhu = db('information')->field('userid,name')->where('type',2)->select();
        $this->assign("banzhu",$banzhu);                    	
		  //初始化curl
        $curl = curl_init();
        // var_dump($curl);

        // 设置APIKEY   URL
        // $apikey = "";//聚合
        $apikey = "1e06dfde5f1247ed910b88697f994206";//易源
        $apiid = "32428";//易源
        $word = urlencode('lol');//%E8%8B%B1%E9%9B%84%E8%81%94%E7%9B%9F
        // echo 'http://v.juhe.cn/toutiao/index?key='.$apikey.'&num=10&keyword='.$word;
        // die;

        // http://route.showapi.com/109-35
        //CURL配置
        curl_setopt($curl, CURLOPT_URL, 'http://route.showapi.com/109-35?showapi_appid='.$apiid.'&showapi_sign='.$apikey.'&title=lol&maxResult=90');

        // curl_setopt($curl, CURLOPT_URL, 'http://v.juhe.cn/toutiao/index?key='.$apikey.'&word='.$word);

        /*'http://api.tianapi.com/keji/?key='.$apikey.'&num=10&word='.$word*/ 
        //将curl_exec()获取的信息 以文件流的形式,而不是直接输出的形式得到
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        //curl执行
        $data = curl_exec($curl);

        //关闭curl
        curl_close($curl);

        //处理JSON数据

        $jsonObj = json_decode($data);
		/*echo "<pre>";
		print_r($jsonObj);
		echo "<pre>";
		DIE;*/
        //提取文章信息
        // $newslist = $jsonObj->newslist;
        // var_dump($jsonObj->showapi_res_body->pagebean->contentlist);
//      echo '<pre>';
//      print_r($jsonObj->showapi_res_body->pagebean->contentlist);
//         var_dump($data);
//      die();
        $this->assign('list', $jsonObj->showapi_res_body->pagebean->contentlist);
        return $this->fetch();
    }
}
